//
//  HLDoctorDetailsViewController.m
//  NewKY
//
//  Created by 罗浩林 on 16/11/14.
//  Copyright © 2016年 Dee. All rights reserved.
//

#import "HLDoctorDetailsViewController.h"
#import <Masonry.h>
#import "UIColor+CZAddition.h"
#import "HMOptinalView.h"
#import "UIView+ZUtility.h"
#import "HLDoctorDetailsModel.h"
#import "HLDoctorView.h"
#import <SVProgressHUD.h>

#define HMSCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define HMSCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#define HMWINDOW [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]
#define HMSYSTEMVERSION_FLOAT [UIDevice currentDevice].systemVersion.floatValue

@interface HLDoctorDetailsViewController ()<HMOptinalViewDelegate,UIScrollViewDelegate>


/*---底部滚动视图---*/
@property (nonatomic,strong) UIScrollView *contentView;
/*---全局访问点---*/
@property (nonatomic,strong) HMOptinalView *btnView;
/*---医生详情模型---*/
@property (nonatomic,strong) HLDoctorDetailsModel *model;
/*---全局访问点---*/
@property (nonatomic,strong) HLDoctorView *doctorInfosView;

@property (nonatomic,strong) UIButton *rightBtn;

@end

@implementation HLDoctorDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor cz_colorWithHex:0xf5f5f5];
    self.navigationItem.title = @"医生基本信息";
    
    self.rightBtn =[[UIButton alloc]init];
    [self.rightBtn setImage:[UIImage imageNamed:@"collection_off"] forState:UIControlStateNormal];
    [self.rightBtn setImage:[UIImage imageNamed:@"collection_on"] forState:UIControlStateSelected];
    [self.rightBtn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    
    self.rightBtn.frame = CGRectMake(0, 0, 30, 30);
    
//    UIBarButtonItem * item = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"HL_collection_off"] style:UIBarButtonItemStyleDone target:nil action:nil];
    
    UIBarButtonItem * barBtn = [[UIBarButtonItem alloc]initWithCustomView:self.rightBtn];
    
    /*---设置关注按钮---*/
//    self.navigationItem.rightBarButtonItem = item;
    self.navigationItem.rightBarButtonItem = barBtn;
    
    /*---创建界面---*/
    [self setupUI];
    /*---加载接口医生基本信息数据---*/
    [self postMethod];
}


/*---加载网络接口数据---*/
- (void)postMethod{
    
    // 获取医生基本信息
            NSString *url = @"http://iosapi.itcast.cn/doctor/getDoctorInfo.json.php";
            NSDictionary *parameters = @{@"user_id":@1000089,@"doctor_id":@300000315};
    
    [NetWorkingManager sendPOSTDataWithPath:url withParamters:parameters withProgress:^(float progress) {
        
    } success:^(BOOL isSuccess, id responseObject) {
        if(isSuccess){
            
            NSDictionary * dict = responseObject[@"data"];
            
            NSLog(@"%@",responseObject);
            
            HLDoctorDetailsModel * model = [HLDoctorDetailsModel modelWithDictionary:dict];
            
            self.doctorInfosView.model = model;
            
        }
    } failure:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    
}

-(void)setupUI{
    
    /*---医生信息View---*/
    _doctorInfosView = [[NSBundle mainBundle]loadNibNamed:@"doctor" owner:nil options:nil].lastObject;
    
//    doctorInfosView.model = self.model;
    
    [self.view addSubview:_doctorInfosView];
    
    /*---按钮视图---*/
    HMOptinalView * btnView= [[HMOptinalView alloc]init];
    btnView.delegate = self;
    
    [self.view addSubview:btnView];
    _btnView = btnView;
    
    
    /*------ 3. 底部 ------*/
    UIScrollView *contentView = [[UIScrollView alloc] init];
    [contentView setBackgroundColor:[UIColor whiteColor]];
    contentView.bounces = NO;
    contentView.showsHorizontalScrollIndicator = NO;
    contentView.pagingEnabled = YES;
    contentView.delegate = self;
    
    [self.view addSubview:contentView];
    _contentView = contentView;
    
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self.view);
        make.top.mas_equalTo(btnView.mas_bottom);
    }];
    
    /*---布局---*/
    [_doctorInfosView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_topMargin);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(90);
    }];
    
    [btnView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_doctorInfosView.mas_bottom).offset(10);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(40);
    }];
    
    /*------ 配置询问的内容视图的界面 ------*/
    [self setupContentView];
    
}

/*---配置内容视图界面---*/
-(void)setupContentView{
    
    
    NSArray *clsNames = @[@"HLJzVC", @"HLSynopsisVC", @"HLTimeVC"];
    
    NSUInteger index = 0;
    for (NSString *claName in clsNames) {
        /*------ 1. 加载三个控制器 ------*/
        Class cls = NSClassFromString(claName);
        UIViewController *vc = [[cls alloc] init];
        
        /*------ 2. 将每个根视图添加到内容视图 ------*/
        // 1. 添加根视图
        [self.contentView addSubview:vc.view];
        // frame值要修改, 不要忘记
        vc.view.frame = self.contentView.bounds;
        
        // 2. 添加控制器
        [self addChildViewController:vc];
        
        // 3. 告诉控制器已经移动添加完成
        [vc didMoveToParentViewController:self];
        
        // 为每个根视图修改位置
        vc.view.x = HMSCREEN_WIDTH * index++;
    }
    
}



- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];
    
    // 此时, 子视图已经布局完成, 自动布局已经生效
    NSLog(@"%@", NSStringFromCGRect(_contentView.frame));
    
    _contentView.contentSize = CGSizeMake(_contentView.width * 3, _contentView.height);
}

#pragma mark - HMOptinalViewDelegate

- (void)optionalView:(HMOptinalView *)view didSelectedIndex:(NSUInteger)index
{
    /*------ optionalView已经做出选择, 让ScollView滚动到对应选项去 ------*/
    
    [_contentView setContentOffset:CGPointMake(index * HMSCREEN_WIDTH, 0) animated:YES];
}

#pragma mark - UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    /*------ 判断contentOffset, 来判断当前的位置 ------*/
    NSUInteger index = scrollView.contentOffset.x / HMSCREEN_WIDTH + 0.5;
    [_btnView selectIndex:index];
}

-(void)clickBtn:(UIButton *)sender{
    
    [SVProgressHUD setMinimumDismissTimeInterval:0.5];
    
    sender.selected = !sender.isSelected;
    
    if (sender.selected) {
        [SVProgressHUD showSuccessWithStatus:@"关注成功"];
    }else{
        [SVProgressHUD showErrorWithStatus:@"取消关注"];
    }
    
    
    
}

@end
